﻿#include "ColorComboBox.h"
#include <QPixmap>

ColorComboBox::ColorComboBox(QWidget *parent)
    :QComboBox(parent)
{

    setIconSize(QSize(16, 16));
    QPixmap pixmap(16, 16);

    pixmap.fill(Qt::black);
    addItem(QIcon(pixmap), QStringLiteral("黑色"), QVariant((int) Qt::black));

    pixmap.fill(Qt::red);
    addItem(QIcon(pixmap), QStringLiteral("红色"), QVariant((int) Qt::red));

    pixmap.fill(Qt::darkRed);
    addItem(QIcon(pixmap), QStringLiteral("深红色"), QVariant((int) Qt::darkRed));

    pixmap.fill(Qt::green);
    addItem(QIcon(pixmap), QStringLiteral("绿色"), QVariant((int) Qt::green));

    pixmap.fill(Qt::darkGreen);
    addItem(QIcon(pixmap), QStringLiteral("深绿色"), QVariant((int) Qt::darkGreen));

    pixmap.fill(Qt::blue);
    addItem(QIcon(pixmap), QStringLiteral("蓝色"), QVariant((int) Qt::blue));

    pixmap.fill(Qt::darkBlue);
    addItem(QIcon(pixmap), QStringLiteral("深蓝色"), QVariant((int) Qt::darkBlue));

    pixmap.fill(Qt::cyan);
    addItem(QIcon(pixmap), QStringLiteral("青绿色"), QVariant((int) Qt::cyan));

    pixmap.fill(Qt::darkCyan);
    addItem(QIcon(pixmap), QStringLiteral("深青绿色"), QVariant((int) Qt::darkCyan));

    pixmap.fill(Qt::magenta);
    addItem(QIcon(pixmap), QStringLiteral("紫红色"), QVariant((int) Qt::magenta));

    pixmap.fill(Qt::darkMagenta);
    addItem(QIcon(pixmap), QStringLiteral("深紫红色"), QVariant((int) Qt::darkMagenta));

    pixmap.fill(Qt::yellow);
    addItem(QIcon(pixmap), QStringLiteral("黄色"), QVariant((int) Qt::yellow));

    pixmap.fill(Qt::darkYellow);
    addItem(QIcon(pixmap), QStringLiteral("深黄色"), QVariant((int) Qt::darkYellow));

    pixmap.fill(Qt::gray);
    addItem(QIcon(pixmap), QStringLiteral("灰色"), QVariant((int) Qt::gray));

    pixmap.fill(Qt::darkGray);
    addItem(QIcon(pixmap), QStringLiteral("深灰色"), QVariant((int) Qt::darkGray));

    pixmap.fill(Qt::lightGray);
    addItem(QIcon(pixmap), QStringLiteral("浅灰色"), QVariant((int) Qt::lightGray));

    connect(this, SIGNAL(currentIndexChanged(int)), this, SLOT(onIndexChanged(int)));
}


void ColorComboBox::onIndexChanged(int)
{
    int colorValue = currentData(Qt::UserRole).toInt();
    QColor cl( (Qt::GlobalColor )colorValue );
    emit colorChanged(cl);
}

ColorComboBox::~ColorComboBox()
{

}

